#include "test.h"
#include "ScopeTimer.h"
#include <opencv2\opencv.hpp>

using namespace cv;
using namespace std;

void Sharpen(const Mat& myImage, Mat& result)
{
	CV_Assert(myImage.depth() == CV_8U);

	result.create(myImage.size(), myImage.type());
	const int nChannels = myImage.channels();

	for (int j = 1; j < myImage.rows-1;++j)
	{
		const unsigned char* previous = myImage.ptr<unsigned char>(j-1);
		const unsigned char* curr = myImage.ptr<unsigned char>(j);
		const unsigned char* next = myImage.ptr<unsigned char>(j+1);

		unsigned char* output = result.ptr<unsigned char>(j);

		for (int i = nChannels; i < nChannels*(myImage.cols-1); ++i)
		{
			*output++ = saturate_cast<unsigned char>(5*curr[i] - curr[i-nChannels] - curr[i+nChannels] - previous[i] - next[i]);
		}
	}

	result.row(0).setTo(Scalar(0));
	result.row(result.rows-1).setTo(Scalar(0));
	result.col(0).setTo(Scalar(0));
	result.col(result.cols-1).setTo(Scalar(0));
}

void Sharpen2(const Mat& image, Mat& result)
{
	Mat kern = (Mat_<char>(3,3) << 0, -1, 0,
								   -1, 5, -1,
								   0, -1, 0);

	filter2D(image, result, image.depth(), kern);
}

int test05(int argc, char** argv)
{
	char* fileName;
	if (argc == 1)
	{
		fileName = "C:\\ProgramsOrTools\\OpenCV2.4.3\\samples\\cpp\\lena.jpg";
	}
	else
	{
		fileName = argv[1];
	}

	Mat image = imread(fileName, 0);
	Mat result;
	namedWindow("test", WINDOW_AUTOSIZE);
	imshow("test", image);
	waitKey(0);

	{
		ScopeTimer timer;
		Sharpen(image, result);
	}

	{
		ScopeTimer timer;
		Sharpen2(image, result);
	}

	imshow("test", result);
	waitKey(0);

	return 0;
}
